package com.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.concurrent.FutureTask;

import com.atguigu.utils.AsposeOfficeUtil;


public class FutureTest2 {
	public static void main(String[] args) {
//第一种		
		PipedOutputStream pout = null;
		PipedInputStream pin = null;
		PipedOutputStream pout2 = null;
		PipedInputStream pin2 = null;
		FileOutputStream fos = null;
		long start = System.currentTimeMillis();
		try {
			pout = new PipedOutputStream();
			pin = new PipedInputStream(pout);
			pout2 = new PipedOutputStream();
			pin2 = new PipedInputStream(pout2);
			fos =new FileOutputStream("E:/office/pdf/wdp5.txt");
			//fileInputStream = new FileInputStream("E:/office/wdp.xlsx");
			FutureTask<?>  f =new FutureTask<Void>(new Task(new FileInputStream("E:/office/wdp.xlsx"), pout,"html"),null);
			new Thread(f).start();
			FutureTask<?>  f2 =new FutureTask<Void>(new Task(pin, pout2,"txt"),null);
			new Thread(f2).start();
			byte[] b=new byte[1024];
			int len=0;
			while((len=pin2.read(b))!=-1) {
				fos.write(b, 0, len);
				fos.flush();
			}		
		} catch (Exception e1) {
			e1.printStackTrace();
		}finally {
			try {
				pin.close(); pout.close(); pin2.close(); pout2.close(); fos.close();
				System.out.println(System.currentTimeMillis() - start);
			} catch (IOException e) { e.printStackTrace(); 
		}
		
//第二种		
//		InputStream  is =null;
//		FileOutputStream fos =null;
//		long start = System.currentTimeMillis();
//		try {
//			is = AsposeOfficeUtil.docment2InputStream("xlsx", new FileInputStream("E:/office/wdp.xlsx") );
//			fos =new FileOutputStream("E:/office/pdf/wdp5.txt");
//			byte[] b=new byte[1024];
//			int len=0;
//			while((len=is.read(b))!=-1) {
//				fos.write(b, 0, len);
//				fos.flush();
//			}		
//		}catch (Exception e) {
//			
//		}finally {
//			try {
//				is.close();
//				fos.close();
//				System.out.println((System.currentTimeMillis()-start));
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
		
			
			
//第三种			
//		PipedOutputStream pout = null;
//		PipedInputStream pin = null;  
//		PipedOutputStream pout2 = null;
//		PipedInputStream pin2 = null;  
//		FileOutputStream fos = null;
//		long c = System.currentTimeMillis();
//		try {
//			pout = new PipedOutputStream();
//			pin = new PipedInputStream(pout);  
//			pout2 = new PipedOutputStream();
//			pin2 = new PipedInputStream(pout2);  
//			fos =new FileOutputStream("E:/office/pdf/wdp.txt");
//			FutureTask<?>  f =new FutureTask<Void>(new Task(new FileInputStream("E:/office/wdp.xlsx"), pout,"html"),null);
//			new Thread(f).start();
//			FutureTask<?>  f2 =new FutureTask<Void>(new Task(pin, pout2,"html"),null);
//			new Thread(f2).start();
//			byte[] b=new byte[1024]; int len=0;
//			while((len=pin2.read(b))!=-1) { fos.write(b, 0, len); fos.flush(); }					
//        }  catch (Exception e) {
//            e.printStackTrace();
//        }finally {     	
//        	try {
//				pin.close(); pout.close(); pin2.close(); pout2.close(); fos.close();
//				System.out.println(System.currentTimeMillis() - c);
//			} catch (IOException e) { e.printStackTrace(); }			
//		}
    }
	
	}
 
//  异步任务
    static class Task implements Runnable {
    	
		private InputStream from;
    	private OutputStream to;
    	private String suffixStr;
        // 返回异步任务的执行结果
        @Override
        public void run()  {
        		try {
        			if(suffixStr.equals("html")) {
        				AsposeOfficeUtil.excel(from, to,suffixStr);
	        		}else if(suffixStr.equals("txt")){
	            		AsposeOfficeUtil.word(from, to,suffixStr);	            		
	            	}
				} catch (Exception e) {
					e.printStackTrace();
				}
        }
        
        public Task() {
			super();
		}
		public Task(InputStream from, OutputStream to,String suffixStr) {
			super();
			this.from = from;
			this.to = to;
			this.suffixStr =suffixStr;
		}
    }
 
}